package com.android.camera.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.media.ThumbnailUtils;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.android.camera.Log;
import com.android.camera.R;

// add from XCRoundRectImageView
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.Color;

public class RGK_XCRoundRectImageView extends TwoStateImageView implements Rotatable {

    private static final String TAG = "XCRoundRectImageView";

    private static final int ANIMATION_SPEED = 270; // 270 deg/sec

    private int mCurrentDegree = 0; // [0, 359]
    private int mStartDegree = 0;
    private int mTargetDegree = 0;

    private boolean mClockwise = false;
    private boolean mEnableAnimation = true;

    private long mAnimationStartTime = 0;
    private long mAnimationEndTime = 0;

    // add from XCRoundRectImageView
    private Paint paint;
    private String paintTextTitle = null;
    private String paintTextItem = null;
    // add end
    int mRoundPx = 50;

    public RGK_XCRoundRectImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint();
        paint.setAntiAlias(true);
    }

    public RGK_XCRoundRectImageView(Context context, int roundPx) {
        this(context, null);

        mRoundPx = roundPx;
    }

    public RGK_XCRoundRectImageView(Context context) {
        this(context, null);

    }

    public RGK_XCRoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
        // super(context, attrs, defStyle);
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    public void setDrawTextTitle(String txt) {
        paintTextTitle = txt;
    }

    public void setDrawText(String txt) {
        paintTextItem = txt;
    }

    // Rotate the view counter-clockwise
    @Override
    public void setOrientation(int degree, boolean animation) {
        Log.d(TAG, "setOrientation(" + degree + ", " + animation + ") mOrientation="
                + mTargetDegree);
        mEnableAnimation = animation;
        // make sure in the range of [0, 359]
        degree = degree >= 0 ? degree % 360 : degree % 360 + 360;
        if (degree == mTargetDegree) {
            return;
        }

        mTargetDegree = degree;
        if (mEnableAnimation) {
            mStartDegree = mCurrentDegree;
            mAnimationStartTime = AnimationUtils.currentAnimationTimeMillis();

            int diff = mTargetDegree - mCurrentDegree;
            diff = diff >= 0 ? diff : 360 + diff; // make it in range [0, 359]

            // Make it in range [-179, 180]. That's the shorted distance between
            // the
            // two angles
            diff = diff > 180 ? diff - 360 : diff;

            mClockwise = diff >= 0;
            mAnimationEndTime = mAnimationStartTime + Math.abs(diff) * 1000 / ANIMATION_SPEED;
        } else {
            mCurrentDegree = mTargetDegree;
        }

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap b;
        Drawable drawable = getDrawable();
        if (drawable == null) {
            Log.e(TAG, "drawable == null, return");
            return;
        }

        Rect bounds = drawable.getBounds();
        int w = bounds.right - bounds.left;
        int h = bounds.bottom - bounds.top;

        if (w == 0 || h == 0) {
            Log.e(TAG, "w == 0 || h == 0, return");
            return; // nothing to draw
        }

        if (mCurrentDegree != mTargetDegree) {
            long time = AnimationUtils.currentAnimationTimeMillis();
            if (time < mAnimationEndTime) {
                int deltaTime = (int) (time - mAnimationStartTime);
                int degree = mStartDegree + ANIMATION_SPEED * (mClockwise ? deltaTime : -deltaTime)
                        / 1000;
                degree = degree >= 0 ? degree % 360 : degree % 360 + 360;
                mCurrentDegree = degree;
                invalidate();
            } else {
                mCurrentDegree = mTargetDegree;
            }
        }

        int left = getPaddingLeft();
        int top = getPaddingTop();
        int right = getPaddingRight();
        int bottom = getPaddingBottom();
        int width = getWidth() - left - right;
        int height = getHeight() - top - bottom;

        int saveCount = canvas.getSaveCount();

        // Scale down the image first if required.
        if ((getScaleType() == ImageView.ScaleType.FIT_CENTER) && ((width < w) || (height < h))) {
            float ratio = Math.min((float) width / w, (float) height / h);
            canvas.scale(ratio, ratio, width / 2.0f, height / 2.0f);
        }
        canvas.translate(left + width / 2, top + height / 2);
        canvas.rotate(-mCurrentDegree);
        canvas.translate(-w / 2, -h / 2);

        // add start
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

        if (mRoundPx == 0) {
            b = bitmap;
        } else {
            b = getRoundBitmap(bitmap, mRoundPx);
        }

        // Bitmap b = bitmap;
        final Rect rectSrc = new Rect(0, 0, width, height);
        final Rect rectDest = new Rect(0, 0, width, height);
        paint.reset();
        canvas.drawBitmap(b, rectSrc, rectDest, paint);

        Log.i(TAG, "width =" + width + ",height =" + height + ",left =" + left + ",right =" + right
                + ",top=" + top + ",bottom=" + bottom);
        int textSize = getResources()
                .getDimensionPixelSize(R.dimen.settings_round_button_title_size);

        if (paintTextTitle != null) {
            paint.setColor(Color.WHITE);

            paint.setTextSize((int) (textSize / 1.3));
            // bug-id:BWLLYBN-534 adapter pain text size for mode ui show
            float textWidth = paint.measureText(paintTextTitle);

            float y = rectDest.bottom + textSize;
            if (paintTextTitle.length() > 10 && paintTextTitle.length() <= 13)
                canvas.drawText(paintTextTitle, rectDest.centerX() - textWidth / 2, y, paint);
            else if (paintTextTitle.length() == 10)
                canvas.drawText(paintTextTitle, rectDest.centerX() - textWidth / 2, y, paint);
            else if (paintTextTitle.length() == 6)
                canvas.drawText(paintTextTitle, rectDest.centerX() - textWidth / 2, y, paint);
            else if (paintTextTitle.length() == 8 || paintTextTitle.length() == 7
                    || paintTextTitle.length() == 9)
                canvas.drawText(paintTextTitle, rectDest.centerX() - textWidth / 2, y, paint);
            else if (paintTextTitle.length() > 13)
                canvas.drawText(paintTextTitle, rectDest.centerX() - textWidth / 2, y, paint);
            else
                canvas.drawText(paintTextTitle, rectDest.centerX() - textWidth / 2, y, paint);
        }

        if (paintTextItem != null) {
            // paint.reset();
            textSize = getResources()
                    .getDimensionPixelSize(R.dimen.settings_round_button_item_size);
            paint.setColor(Color.WHITE);
            paint.setTextSize((int) (textSize / 1.3));

            if (paintTextItem.length() >= 9)
                canvas.drawText(paintTextItem, rectDest.centerX() - width / 3, rectDest.bottom,
                        paint);
            else
                canvas.drawText(paintTextItem, rectDest.centerX() - width / 5, rectDest.bottom,
                        paint);
        }
        // add end
        // add end

        // remove
        // drawable.draw(canvas);

        canvas.restoreToCount(saveCount);
    }

    private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;

        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        int x = bitmap.getWidth();

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;

    }

    private Bitmap mThumb;
    private Drawable[] mThumbs;
    private TransitionDrawable mThumbTransition;

    public void setBitmap(Bitmap bitmap) {
        // Make sure uri and original are consistently both null or both
        // non-null.
        if (bitmap == null) {
            mThumb = null;
            mThumbs = null;
            setImageDrawable(null);
            // setVisibility(GONE);
            return;
        }

        LayoutParams param = getLayoutParams();
        final int miniThumbWidth = param.width - getPaddingLeft() - getPaddingRight();
        final int miniThumbHeight = param.height - getPaddingTop() - getPaddingBottom();
        mThumb = ThumbnailUtils.extractThumbnail(bitmap, miniThumbWidth, miniThumbHeight);
        if (mThumbs == null || !mEnableAnimation) {
            mThumbs = new Drawable[2];
            mThumbs[1] = new BitmapDrawable(getContext().getResources(), mThumb);
            setImageDrawable(mThumbs[1]);
        } else {
            mThumbs[0] = mThumbs[1];
            mThumbs[1] = new BitmapDrawable(getContext().getResources(), mThumb);
            mThumbTransition = new TransitionDrawable(mThumbs);
            setImageDrawable(mThumbTransition);
            mThumbTransition.startTransition(500);
        }
        setVisibility(VISIBLE);
    }

}
